package com.szrt.cookie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;

/**
 * Servlet implementation class CookieController
 */
@WebServlet("/CookieController")
public class CookieController extends HttpServlet {
	private static final long serialVersionUID = 1L;

       
	@SuppressWarnings("unused")
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	  
	  
	   
		   Cookie[] cookies = req.getCookies();
		   String passLogin=req.getParameter("passLogin");
		   String username=req.getParameter("username");
		   String password=req.getParameter("password");
		   
		   if(null !=passLogin && "on".equals(passLogin)){
			   
			   Cookie usernameCookie = new Cookie("username", username);
			   Cookie passwordCookie = new Cookie("password", password);
			   
			   usernameCookie.setMaxAge(60);
			   passwordCookie.setMaxAge(60);
			   
			   resp.addCookie(passwordCookie);
			   resp.addCookie(usernameCookie);
		   }else{
			   
			   if(cookies.length>=2){
				   username= (String)req.getAttribute("username");
				   password= (String)req.getAttribute("password");
			   }
		   }
	   
	   if("scott".equals(username)&& "123".equals(password) ){
		   
		  //登陆成功 
		   
		   req.getRequestDispatcher("success.jsp").forward(req, resp);
		   
	   }else{
		   
		   req.getRequestDispatcher("error.jsp").forward(req, resp);
		   
	   }
	   
	   
}

}
